﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.订单一览新.辅料.service;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.生产计划.生产批生产.service;
using mendian;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.项目成果管理.service;
using DXApplicationTangche.UC.全部工序.service;
using DXApplicationTangche.UC.货号管理;
using DXApplicationTangche.UC.下单MTO.Service;

namespace DXApplicationTangche.UC.全部工序.面辅料.辅料相关
{
    public partial class Form辅料工序 : DevExpress.XtraEditors.XtraForm
    {
        private String assertNo;//设备码
        private String barcode { get; set; }//水洗标
        private String code { get; set; }//工序码
        private String batchNo;//生产批号
        private String mnemonic;//工号
        private OrderCommitDTO返回 orderCommitDTO = new OrderCommitDTO返回();//运维口取的订单数据
        private List<StyleAccessoryDTO> styleAccessoryDTOs = new List<StyleAccessoryDTO>();
        private List<DTO辅料显示> dTO辅料显示s = new List<DTO辅料显示>(); //批号和工序code查询的辅料
        public Form辅料工序(WBSItemDTO wBSItemDTO)
        {
            InitializeComponent();
            this.assertNo = wBSItemDTO.assertNo;
            this.code = wBSItemDTO.code;
            this.Text = wBSItemDTO.activityDescription;
        }
        public Form辅料工序(WBSItemDTO wBSItemDTO,String batchNo,String mnemonic)
        {
            InitializeComponent();
            // 当前工序
            this.assertNo = wBSItemDTO.assertNo;
            this.code = wBSItemDTO.code;
            this.Text = wBSItemDTO.activityDescription;
            // 
            this.textBox生产批号.Enabled = false;
            this.textBox水洗标.Enabled = false;
            this.textBox工号.Enabled = false;
            this.simpleButton操作人退出.Enabled = false;
            this.batchNo = batchNo;
            this.label消息.Text = "消息：" + this.batchNo;
            this.mnemonic = mnemonic;
            this.label当前操作人.Text = "当前操作人：" + this.mnemonic;
            try
            {
                String json = RestCall.httpGetMethod(URL.接口url + "/pmc/getOrderCacheByOrderId?batchNo=" + this.batchNo);
                this.orderCommitDTO = FunctionHelper.JsonDeserialization<OrderCommitDTO返回>(json);
                this.styleAccessoryDTOs = new 生产批生产Service(this.batchNo, this.code).styleAccessoryDTOs;
                this.dTO辅料显示s = new 生产批生产Service(this.batchNo, this.code).dTO辅料显示s;
                this.showTextBoxs();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            if (!String.IsNullOrEmpty(this.mnemonic))
            {
                //生产批生产Service.post扫码(
                //      this.mnemonic,
                //      this.batchNo,
                //      this.code
                //      );
                //提交时保存成果物
                生产批生产Service.post工序扫码(
                      this.mnemonic,
                      this.batchNo,
                      this.code,
                      new ProjectAchievementDTO(
                          Const工序.accessory,
                          this.styleAccessoryDTOs
                          )
                      );
                this.textBox生产批号.SelectAll();
            }
        }
        private void textBox水洗标_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrEmpty(this.textBox水洗标.Text.Trim()))
                {
                    return;
                }
                this.barcode = this.textBox水洗标.Text.Trim();
                this.batchNo = 订单预览新Service.getBatchNoByBarcode(this.textBox水洗标.Text.Trim());
                if (String.IsNullOrEmpty(this.batchNo))
                {
                    MessageBox.Show("请在生产批位置扫描生产批号");
                    return;
                }
                this.查询();
            }
        }

        private void textBox生产批号_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrWhiteSpace(this.textBox生产批号.Text))
                {
                    MessageBox.Show("请扫描生产批号");
                    return;
                }
                this.batchNo = this.textBox生产批号.Text.Trim();
                this.查询();
            }
        }
        private void 查询()
        {
            this.clearTextBoxs();
            this.label消息.Text = "消息：" + this.batchNo;
            this.mnemonic = 生产批生产Service.get查找设备session对应的员工(assertNo);
            this.label当前操作人.Text = "当前操作人：" + this.mnemonic;
            try
            {
                String json = RestCall.httpGetMethod(URL.接口url + "/pmc/getOrderCacheByOrderId?batchNo=" + this.batchNo);
                this.orderCommitDTO = FunctionHelper.JsonDeserialization<OrderCommitDTO返回>(json);
                this.styleAccessoryDTOs = new 生产批生产Service(this.batchNo, this.code).styleAccessoryDTOs;
                this.dTO辅料显示s = new 生产批生产Service(this.batchNo, this.code).dTO辅料显示s;
                this.showTextBoxs();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            if (!String.IsNullOrEmpty(this.mnemonic))
            {
                try
                {
                    //  工序顺序提醒，不影响继续干活
                    UC.下单MTO.Service.PlainMessageResult plainMessageResult =
                        生产批生产Service.validateProjectInstance(
                            this.batchNo,
                            this.code
                            );
                    if (!ConstPlainMessageResult.OK.Equals(plainMessageResult.errorCode))
                    {
                        MessageBox.Show(plainMessageResult.errorMessage);
                    }
                }
                catch { }
                //提交时保存成果物
                生产批生产Service.post工序扫码(
                      this.mnemonic,
                      this.batchNo,
                      this.code,
                      new ProjectAchievementDTO(
                          Const工序.accessory,
                          this.styleAccessoryDTOs
                          )
                      );
                this.textBox生产批号.SelectAll();
            }
            else
            {
                this.label消息.Text = "消息：请扫描您的工号!";
                this.ActiveControl = this.textBox工号;
            }
        }

        private void textBox工号_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrEmpty(this.mnemonic))
                {
                    if (String.IsNullOrWhiteSpace(项目成果Service.get判断扫描员工码是否正确(this.textBox工号.Text.Trim())))
                    {
                        this.textBox工号.Clear();
                        MessageBox.Show("扫描员工码错误，请重新扫描");
                        return;
                    }
                    this.mnemonic = this.textBox工号.Text.Trim();
                    生产批生产Service.save保存设备session(
                    this.assertNo,
                    this.mnemonic
                    );
                    this.label消息.Text = "消息：";
                    this.label当前操作人.Text = "当前操作人：" + this.mnemonic;
                }

                try
                {
                    try
                    {
                        //  工序顺序提醒，不影响继续干活
                        UC.下单MTO.Service.PlainMessageResult plainMessageResult =
                            生产批生产Service.validateProjectInstance(
                                this.batchNo,
                                this.code
                                );
                        if (!ConstPlainMessageResult.OK.Equals(plainMessageResult.errorCode))
                        {
                            MessageBox.Show(plainMessageResult.errorMessage);
                        }
                    }
                    catch { }
                    //提交时保存成果物
                    生产批生产Service.post工序扫码(
                          this.mnemonic,
                          this.batchNo,
                          this.code,
                          new ProjectAchievementDTO(
                              Const工序.accessory,
                              this.styleAccessoryDTOs
                              )
                          );
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                this.clearTextBoxs();
                this.ActiveControl = this.textBox生产批号;
            }
        }
        /// <summary>
        /// 显示信息
        /// </summary>
        private void showTextBoxs()
        {
            this.textBox订单号.Text = this.orderCommitDTO.orderNo;
            this.textBox订单数量.Text = this.orderCommitDTO.orderNumber.ToString();
            this.textBox下单时间.Text = this.orderCommitDTO.orderDate.ToString();
            this.textBox客户名称.Text = this.orderCommitDTO.customerName;
            this.textBox店铺名称.Text = this.orderCommitDTO.shopName;
            this.textBox款式名称.Text = this.orderCommitDTO.styleNameCn;
            this.textBox面料号.Text = this.orderCommitDTO.materialCode;
            this.textBox面料名称.Text = this.orderCommitDTO.materialNameCn;
            this.textBox面料颜色.Text = this.orderCommitDTO.materialColor;
            this.textBox面料成分.Text = this.orderCommitDTO.composition;
            if (this.dTO辅料显示s.Count == 0)
            {
                this.xtraTabPage辅料.PageVisible = false;
            }
            else 
            {
                this.xtraTabPage辅料.PageVisible = true;
            }
            this.gridControl辅料.DataSource = this.dTO辅料显示s;
        }
        /// <summary>
        /// 清空
        /// </summary>
        private void clearTextBoxs()
        {
            this.textBox生产批号.Clear();
            this.textBox水洗标.Clear();
            this.textBox工号.Clear();
            this.textBox订单号.Clear();
            this.textBox订单数量.Clear();
            this.textBox下单时间.Clear();
            this.textBox客户名称.Clear();
            this.textBox店铺名称.Clear();
            this.textBox款式名称.Clear();
            this.textBox面料号.Clear();
            this.textBox面料名称.Clear();
            this.textBox面料颜色.Clear();
            this.textBox面料成分.Clear();
            this.label消息.Text = "消息：";
            this.dTO辅料显示s.Clear();
            this.gridView辅料.RefreshData();
        }

        private void simpleButton操作人退出_Click(object sender, EventArgs e)
        {
            this.clearTextBoxs();
            this.ActiveControl = this.textBox生产批号;
            this.mnemonic = "";
            生产批生产Service.save保存设备session(
                    this.assertNo,
                    this.mnemonic
                    );
            this.label当前操作人.Text = "当前操作人：" + this.mnemonic;
        }

        
    }
}